import { _decorator, Component, input, instantiate, Label, Node, ProgressBar, Texture2D, tween, Tween, Vec3 } from 'cc';
import DataManager from '../../Global/DataManager';
import { EntityTypeEnum, IActor, InputTypeEnum, toFixed } from '../../Common';
import { EntityManager } from '../../Base/EntityManager';
import { ActorStateMachine } from './ActorStateMachine';
import { EntityStateEnum, EventEnum } from '../../Enum';
import { WeaponManager } from '../Weapon/WeaponManager';
import { rad2Angle } from '../../Utils';
import EventManager from '../../Global/EventManager';
const { ccclass, property } = _decorator;

@ccclass('ActorManager')
export class ActorManager extends EntityManager {

    bulletType:EntityTypeEnum
    private wm:WeaponManager;
    private id:number;

    private hp:ProgressBar;//生命
    private playerNameID:Label;//名字
    private targetPos:Vec3;
    private tw:Tween<unknown>;
    //初始化
   init(data:IActor ){
    this.hp=this.node.getComponentInChildren(ProgressBar);
    this.playerNameID=this.node.getChildByName("Name").getComponent(Label);
    if(!this.playerNameID){
        console.error("没找到 Name");
    }
    this.bulletType=data.bulletType;
    this.fsm=this.addComponent(ActorStateMachine);
    this.fsm.init(data.type);

    this.state=EntityStateEnum.Idle;
    this.id=data.id;
    this.node.active=false;//隐藏角色 防止闪烁
    this.targetPos=undefined

    //创建武器
    const prefab=DataManager.Instance.prefabMap.get(EntityTypeEnum.Weapon1);
    const weapon=instantiate(prefab);
    weapon.setParent(this.node);
    const wm=weapon.addComponent(WeaponManager);
    wm.init(data);

    this.wm=wm;

   }

    tick(dt: number) {
        if(this.id!=DataManager.Instance.myPlayerId){
            return;
        }

       if(DataManager.Instance.jm.input.length()>0.001){
        const inputPos=DataManager.Instance.jm.input.normalize();
        EventManager.Instance.emit(EventEnum.ClientSyn,{      
            id:DataManager.Instance.myPlayerId,
            type:InputTypeEnum.ActorMove,//输入类型
            direction:{
                x:toFixed(inputPos.x),
                y:toFixed(inputPos.y),
                z:0
            },//输入方向
            dt:toFixed(dt),
         })

       }
       else{
   
       }
    }
    //更新角色状态
    render(data:IActor){

        this.renderPos(data);
        this.renderDire(data);
        this.renderHp(data);
        this.renderName(data);
    }
    renderPos(data:IActor){

        const {direction,position}=data;
        // this.node.setPosition(position.x,position.y,position.z);//设置位置
        const newPos=new Vec3(position.x,position.y,position.z);
        if(!this.targetPos){
            this.node.active=true;
            this.node.setPosition(newPos);
            this.targetPos=new Vec3(newPos);
        }else if(!this.targetPos.equals(newPos)){//只有位置改变大于0.1才缓动
            this.tw?.stop();//开始缓动前停止其他缓动
            this.node.setPosition(this.targetPos);
            this.targetPos.set(newPos);
            this.state=EntityStateEnum.Run;
            this.tw=tween(this.node)
            .to(0.1,{position:this.targetPos})
            .call(()=>{
                this.state=EntityStateEnum.Idle;
            })  
            .start();
        }
    }

    renderDire(data:IActor){
        const {direction,position}=data;
        if(direction.x!=0){
            this.node.setScale(direction.x>0?1:-1,1,1);
            this.hp.node.setScale(direction.x>0?1:-1,1,1);
            this.playerNameID.node.setScale(direction.x>0?1:-1,1,1);
        }
        const side=Math.sqrt(direction.x*direction.x+direction.y*direction.y);
        const rad =Math.asin(direction.y/side);
        const angle=rad2Angle(rad);
        this.wm.node.setRotationFromEuler(0,0,angle);
    }
    renderHp(data:IActor){
        this.hp.progress=data.hp/this.hp.totalLength;
    }
    renderName(data:IActor){
        

        if(this.id==DataManager.Instance.myPlayerId){
            let string="(我)"+data.nickname.toString();
            this.playerNameID.string=string;    
        }
        else{
            this.playerNameID.string=data.nickname.toString();
        }

    }
}

